import groovy.beans.Bindable

class RemoteFileViewerModel {
	//set by TermController
	def mvcId
	def parentController

   @Bindable String currentFolder="."
   @Bindable def folderContents=[]
   @Bindable boolean anySelected=false
   def term

   void loadContents(Vector<String> vv)
   {
        folderContents= []
        def filesWithin=[]
        if(vv!=null){
          for(int ii=0; ii<vv.size(); ii++)
          {
            Object obj=vv.elementAt(ii);

            if(obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry)
            {
                com.jcraft.jsch.ChannelSftp.LsEntry entry = ((com.jcraft.jsch.ChannelSftp.LsEntry)obj)
                if(entry.getFilename()!='.')
                {
                    //entry.getAttrs()
                    def item = [type:(entry.getAttrs().isDir() ? 'D' : 'F'),name:entry.getFilename(),longname:entry.getLongname(),entry:entry]
                    if(entry.getAttrs().isDir())
                    {
                        folderContents<< item
                    } else {
                        filesWithin<< item
                    }
                }
            }
          }
          def comparator = [compare: { o1,o2 -> return o1.name.compareTo(o2.name) }] as Comparator;
          Collections.sort(folderContents, comparator)
          Collections.sort(filesWithin, comparator)
          folderContents.addAll(filesWithin) //files at the end
        }

   }
}